package com.practice.designMode.responsibilityChain.validation;

import com.practice.designMode.responsibilityChain.exception.ValidatorException;

import java.util.ArrayList;
import java.util.List;

public class ValidatorContext {

    private final List<String> errorMessageList = new ArrayList<>();

    // 赋予每一个处理器节点停止责任链的能力
    private boolean stop = false;

    // 赋予每一个处理器决定是否继续执行的能力
    private int index = 0;

    // 甚至可以做到赋予每一个处理器节点对校验内容修改的能力
    private Object value;

    public ValidatorContext(Object value){
        this.value = value;
    }

    public Object getValue() {
        return value;
    }

    public void appendError(String message){
        errorMessageList.add(message);
    }

    public void doNext(Object value){
        index++;
        this.value = value;
    }
    public int CurrentIndex(){
        return index;
    }

    public void stopChain(){
        stop = true;
    }

    public boolean shouldStop(){
        return stop;
    }

    public void throwExceptionIfNecessary(){
        if(errorMessageList.isEmpty()){
            return;
        }
        throw new ValidatorException(errorMessageList.toString());
    }
}
